<template>
  <div class="course-management">
    <div class="header">
      <h2>课程管理</h2>
      <el-button type="primary" @click="showCreateDialog">创建新课程</el-button>
    </div>

    <el-table :data="courses" style="width: 100%">
      <el-table-column prop="title" label="课程名称" />
      <el-table-column prop="price" label="价格" width="120" />
      <el-table-column prop="published" label="状态" width="120">
        <template #default="{ row }">
          <el-tag :type="row.published ? 'success' : 'info'">
            {{ row.published ? '已发布' : '草稿' }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="250">
        <template #default="{ row }">
          <el-button-group>
            <el-button @click="editCourse(row)">编辑</el-button>
            <el-button
              :type="row.published ? 'warning' : 'success'"
              @click="togglePublish(row)"
            >
              {{ row.published ? '取消发布' : '发布' }}
            </el-button>
            <el-button type="danger" @click="deleteCourse(row)">删除</el-button>
          </el-button-group>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog
      v-model="dialogVisible"
      :title="isEdit ? '编辑课程' : '创建课程'"
      width="50%"
    >
      <el-form :model="courseForm" label-width="120px">
        <el-form-item label="课程名称" required>
          <el-input v-model="courseForm.title" />
        </el-form-item>
        <el-form-item label="课程描述">
          <el-input
            v-model="courseForm.description"
            type="textarea"
            rows="3"
          />
        </el-form-item>
        <el-form-item label="课程价格" required>
          <el-input-number
            v-model="courseForm.price"
            :min="0"
            :precision="2"
          />
        </el-form-item>
        <el-form-item label="课程分类">
          <el-select
            v-model="courseForm.categories"
            multiple
            placeholder="选择分类"
          >
            <el-option
              v-for="category in categories"
              :key="category"
              :label="category"
              :value="category"
            />
          </el-select>
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="saveCourse">保存</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script lang="ts" setup>
import { ref, onMounted } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import {
  getCoursesByInstructor,
  createCourse,
  updateCourse,
  deleteCourse as apiDeleteCourse
} from '@/api/courses'

const courses = ref([])
const dialogVisible = ref(false)
const isEdit = ref(false)
const courseForm = ref({
  id: null,
  title: '',
  description: '',
  price: 0,
  categories: []
})

const loadCourses = async () => {
  const result = await getCoursesByInstructor()
  courses.value = result.data
}

const showCreateDialog = () => {
  isEdit.value = false
  courseForm.value = {
    id: null,
    title: '',
    description: '',
    price: 0,
    categories: []
  }
  dialogVisible.value = true
}

const editCourse = (course) => {
  isEdit.value = true
  courseForm.value = { ...course }
  dialogVisible.value = true
}

const saveCourse = async () => {
  try {
    if (isEdit.value) {
      await updateCourse(courseForm.value.id, courseForm.value)
    } else {
      await createCourse(courseForm.value)
    }
    ElMessage.success('保存成功')
    dialogVisible.value = false
    loadCourses()
  } catch (error) {
    ElMessage.error('保存失败')
  }
}

const deleteCourse = async (course) => {
  try {
    await ElMessageBox.confirm('确定要删除该课程吗？')
    await apiDeleteCourse(course.id)
    ElMessage.success('删除成功')
    loadCourses()
  } catch (error) {
    if (error !== 'cancel') {
      ElMessage.error('删除失败')
    }
  }
}

onMounted(() => {
  loadCourses()
})
</script>

<style scoped>
.course-management {
  padding: 20px;
}

.header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}
</style>